package cn.yeyiu.netonlinevideo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import java.util.List;

import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.netonlinevideo.activity.DataListActivity;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.util.bean.MediaData;

public class TypeListAdapter extends RecyclerView.Adapter<TypeListAdapter.ViewHolder> {

    private final Context mContext;
    private List<TypeData> typeList;

    public TypeListAdapter(Context context) {
        mContext = context;
    }


    public void updateList(List<TypeData> typeDataList) {
        this.typeList = typeDataList;
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(mContext).inflate(R.layout.item_data, parent, false);
        return new TypeListAdapter.ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TypeData type = typeList.get(position);
        holder.title.setText(type.name);
        Glide.with(mContext)
                .load(type.logo)
                .into(holder.img);

        holder.itemView.setOnClickListener(v->{
            Intent i = new Intent(mContext, DataListActivity.class);
            i.putExtra("type", JSONObject.toJSONString(type));
            mContext.startActivity(i);
        });
    }

    @Override
    public int getItemCount() {
        if (typeList == null) {
            return 0;
        } else {
            return typeList.size();
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView img;
        public TextView title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.data_pic_img);
            title = itemView.findViewById(R.id.data_title_tv);
        }
    }
}
